<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { accountApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  import serachUserid from '@/components/serachUserid/index.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:''
  })
  interface formQuert {
    uuid:number | string
    user_id:number | string
    change_diamond:number
    sys_notice:string
    desc:string
    img:string
    type:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    user_id: [{ required: true, trigger: 'blur', message: '请选择用户' }],
    change_diamond: [{ required: true, trigger: 'blur', message: '请输入金额' }],
    desc: [{ required: true, trigger: 'blur', message: '请输入说明' }],
    sys_notice: [{ required: true, trigger: 'blur', message: '请输入系统通知' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      form.user_id = form.user_id.toString()
      form.uuid = form.uuid.toString()
      await accountApi.setfundList(form).then(res=>{
        loading.close()
        ToastSuccess(action.title + '成功')
        $emit('updateList')
        closeDialog()
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = (query:any={})=> {
    const { type } = query
    form.type = type
    action.title = `钻石${['','扣款','赠送'][type]}`
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="8vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="50%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="用户UUID" prop="user_id">
          <serachUserid v-model:uuid="form.uuid" v-model:user_id="form.user_id" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="金额" prop="change_diamond">
          <el-input type="number" v-model="form.change_diamond" placeholder="请输入金额"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="说明" prop="desc">
          <el-input
            :disabled="action.disabled"
            v-model="form.desc"
            :autosize="{ minRows: 4, maxRows: 4}"
            type="textarea"
            placeholder="请输入说明"
          />
        </el-form-item>
        <el-form-item v-if="form.type == 2" :label-width="formLabelWidth" label="系统通知" prop="sys_notice">
          <el-input
            :disabled="action.disabled"
            v-model="form.sys_notice"
            :autosize="{ minRows: 4, maxRows: 4}"
            type="textarea"
            placeholder="请输入系统通知"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="凭证">
          <xmUpload listType="picture-card" :data="{type:26}" v-model:file-url="form.img"/>
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>